import hashlib
import requests
from datetime import datetime

from common.conf import Conf


class Tool:
    def __init__(self):
        self.c = Conf()

    def from_camId_chanId_to_cid(self, camId, chan):
        # camid is nvr or camera ; chan is chan in camera
        camid = str(camId)[-6:]
        chan = str(chan)
        if len(chan)>2:chan=chan[-2:]
        elif len(chan) == 1: chan = "0" + chan
        cid = f"{camid}{chan}"
        # print("cid=======", cid)
        return cid

    def cid2ssrc(self, cid):
        y = int(cid)
        ssrc = str(hex(y))[2:].upper()
        n = 8 - len(ssrc)
        prefix = ["0" for i in range(n)]
        ssrc = "".join(prefix) + ssrc
        return ssrc

    def from_camId_chanId_to_ssrc(self, camId, chan):
        cid=self.from_camId_chanId_to_cid(camId,chan)
        ssrc=self.cid2ssrc(cid)
        return ssrc

    def line_by_word(self, s, k):
        ls = s.split("\n")
        for s in ls:
            if k in s:
                s = s.replace(k, "")
                s = s.strip()
                return s
        return ""

    def now(self):
        now = datetime.now()  # current date and time
        t = now.strftime("%Y-%m-%dT%H:%M:%S")
        return t

    def md5(self):
        # initializing string
        call_id = '214776626:'
        did = '34020000001110000003'
        s = call_id + did
        result = hashlib.md5(s.encode())
        print(result.hexdigest())

    def hex(self, s):
        n = int(s)
        print(hex(n))
        return hex(n)





if __name__ == '__main__':
    t = Tool()
    msg = '''REGISTER sip:34020000002000000001@3402000000 SIP/2.0\r\nVia: SIP/2.0/UDP 
    192.168.0.120:5060;rport;branch=z9hG4bK688377620\r\nFrom: 
    <sip:34020000001320000001@3402000000>;tag=1677830343\r\nTo: <sip:34020000001320000001@3402000000>\r\nCall-ID: 
    78917237\r\nCSeq: 1 REGISTER\r\nContact: <sip:34020000001320000001@192.168.0.120:5060>\r\nMax-Forwards: 
    70\r\nUser-Agent: IP Camera\r\nExpires: 3600\r\nContent-Length: 0\r\n\r\n'''
    # t.open_rtp("39625744")
    # t.close_rtp()
    # t.get_rtp_info()
